/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.controller.user;

import akka.actor.ActorRef;
import com.cxhd.pacs.StaticData;
import com.cxhd.pacs.actor.ActorManager;
import com.cxhd.pacs.controller.service.AskService;
import com.cxhd.pacs.external.IntefaceOther;
import com.cxhd.pacs.model.Asklist;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author 52461
 */
public class AskCtrl extends Controller {

    AskService as = new AskService();

    public void index() {
        try {

            setAttr("info", JsonKit.toJson(Db.findFirst("select a.*,b.name as hospital from hostinfo a left join hospital b on a.hospital_code=b.hospital_code where host_id='" + getPara("host_id") + "'")));
            setAttr("type", getPara("type"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        render("/view/ask/apply.jsp");
    }

    public void viewApply() {

        setAttr("ask_code", getPara("ask_code"));
        setAttr("type", getPara("type"));
        render("/view/ask/apply.jsp");
    }

    public void saveApply() {
        Map<String, String> map = new Gson().fromJson(getPara("data"), new TypeToken<Map<String, String>>() {
        }.getType());
        System.out.println(map);
        renderJson(as.createAsk(map, this));

    }

    public void getInfo() {
        String type = getPara("type");
        if (type.equalsIgnoreCase("pacs")) {
            type = "'CT','DX','MRI','US','MG','DR'";
        } else {
            type = "'" + type + "'";
        }
        //System.out.println("select * from host_detail where event_name in(" + type + ") and host_id='" + getPara("host_id") + "'");
        renderJson(Db.find("select * from host_detail where event_name in(" + type + ") and host_id='" + getPara("host_id") + "'"));
    }

    public void getDetail() {
        Map<String, Object> map = new Gson().fromJson(Db.findFirst("select * from ask_detail where ask_code='" + getPara("ask_code") + "'").getStr("datail"), new TypeToken<Map<String, Object>>() {
        }.getType());
        Record re = Db.findFirst("select * from asklist where ask_code='" + getPara("ask_code") + "'");
        if (re != null) {
            map.put("appointment_time", re.get("appointment_time"));
            map.put("recvicer_phone", re.getStr("recvicer_phone"));
            map.put("recvicer_name", re.getStr("recvicer_name"));
            map.put("recvicer_id", re.getStr("recvicer_id"));
            map.put("ask_code", re.get("ask_code") + "");
        }
        //System.out.println(map);
        renderJson(map);

    }

    public void getDoctor() {

        renderJson(IntefaceOther.getDoctorByGroup(StaticData.DOCTOR_GROUP_NAME));

        //renderJson(IntefaceOther.getCurrentDayDoctor(getPara("date")));
    }

    public void updateAskDetail() {
        Asklist rd = new Asklist();
        Map<String, Object> loginUserInfo = (Map<String, Object>) getSessionAttr("CX_JSESSION_USER");
        String data = as.updateAskRecvier(getPara("recvicer_id"), getPara("recvicer_phone"), getPara("recvicer_name"), getPara("appointment_time"), loginUserInfo.get("username") + "", loginUserInfo.get("phone") + "", loginUserInfo.get("ch_name") + "", getPara("ask_code"));
        if (data != null && data.equalsIgnoreCase("true")) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("msgType", "sendDoctorAndsender");
            for (String key : getParaMap().keySet()) {
                map.put(key, getPara(key));
            }
            map.put("patient_name", getPara("name"));
            ActorManager.getIntance().getActor("sendSmsActor").tell(map, ActorRef.noSender());
        }
        renderText(data);
    }

}
